/*------------------------------------------------------------------------------

Publication: Schakel, W. (2020) Representing the Rich: Economic and Political
	Inequality in Established Democracies. University of Amsterdam: Doctoral
	Dissertation.

Chapter: 4

Description: This do-file contains the commands for the figures presented in
	chapter 4.

Sections:

	1. Figure 4.1 (mean spending preferences of low and high incomes)
	2. Figure 4.2 (predicted values of party attention by spending preferences)
	3. Figure 4.3 (responsiveness of all, left and right parties)

Data: Schakel_2020_Chapter4_1_Data (see Schakel_2020_Chapter4_2_Preparation
	do-file for original sources)

------------------------------------------------------------------------------*/

global data "YOUR PATH HERE"

use "$data\Schakel_2020_Chapter4_1_Data.dta", clear

/*------------------------------------------------------------------------------
 1. Figure 4.1 (mean spending preferences of low and high incomes)
------------------------------------------------------------------------------*/

tabstat p10 p90 if growtht!=., by(topic) s(mean semean) c(stat) nototal save

matrix define y1_=(r(Stat1),r(Stat2),r(Stat3),r(Stat4),r(Stat5),r(Stat6))'
svmat y1_

gen lowbar=y1_1-y1_2
gen highbar=y1_1+y1_2

* This matrix contains the values of the x-axis and the income groups
matrix define x1_=(13,14,16,17,1,2,7,8,10,11,4,5\1,2,1,2,1,2,1,2,1,2,1,2)'
svmat x1_

twoway (bar y1_1 x1_1 if x1_2==1, fc(gs8) fi(100) lc(gs8))					///
	(bar y1_1 x1_1 if x1_2==2, fc(gs13) fi(100) lc(gs13))					///
	(rcap lowbar highbar x1_1, lc(black) msize(zero)),						///
	xt("") yt("Mean", size(medium))											///
	xlab(1.5 "Education" 4.5 "Welfare" 7.5 "Environment" 10.5 "Crime"		///
	13.5 "Culture" 16.5 "Defense", labgap(tiny) notick)						///
	legend(order(1 "10th percentile" 2 "90th percentile") bm(medium)		///
	symx(8) size(medsmall)) plotr(m(4 3 0 0) lc(white))						///
	graphr(m(2 2 0 2) fc(white) lc(white)) ylab(-20(20)60, ang(hor) nogrid)	///
	yline(0, lc(gs0) lw(thin)) xsiz(6.7) ysiz(5)

drop y1_1-x1_2

/*------------------------------------------------------------------------------
 2. Figure 4.2 (predicted values of party attention by spending preferences)
------------------------------------------------------------------------------*/

use "$data\Schakel_2020_Chapter4_1_Data.dta", clear

qui mixed llegpit1 p10 p90 loggdpt growtht unempt i.topic i.wave || country:, vce(rob)

forval i=10(80)90 {
	qui margins, at(p`i'=(-59(2)79)) atmeans

	matrix pred`i'=r(b)'
	matrix rv`i'=r(V)
	matrix se`i'=J(70,1,.)

	forval j=1/70 {
		matrix se`i'[`j',1]=sqrt(rv`i'[`j',`j'])
		}

	svmat pred`i'
	svmat se`i'
	gen lowci`i'=pred`i'-1.96*se`i'
	gen highci`i'=pred`i'+1.96*se`i'
	}

range x -59 79 70

twoway (line pred10 x, lc(gs11) lw(medthick))								///
	(rline highci10 lowci10 x, lc(gs11) lp(dash))							///
	(line pred90 x, lc(gs0) lw(medthick))									///
	(rline highci90 lowci90 x, lc(gs0) lp(dash)								///
	legend(order(1 "10th percentile" 3 "90th percentile") symx(8)			///
	size(medium)) xt("Spending preferences", m(0 0 0 1) size(medium))		///
	yt("Average logged attention (t+1)", m(zero) size(medium))				///
	xlab(-60(20)80) ylab(-1(1)4, angle(hor) nogrid)							///
	plotr(lc(white)) graphr(m(2 4 0 2) fc(white) lc(white)))

drop pred101-x

/*------------------------------------------------------------------------------
 3. Figure 4.3 (responsiveness of all, left and right parties)
------------------------------------------------------------------------------*/

use "$data\Schakel_2020_Chapter4_1_Data.dta", clear

matrix m_all=J(17,1,.)
matrix m_left=J(17,1,.)
matrix m_right=J(17,1,.)

local counter1=1
local counter2=1
local counter3=1

quietly forval i=10(5)90 {
	mixed llegpit1 p`i' loggdpt growtht unempt i.topic i.year || country:, vce(rob)
	matrix b=e(b)
	matrix m_all[`counter1',1]=b[1,1]
	local ++counter1

	mixed lleftpit1 p`i' loggdpt growtht unempt i.topic i.year || country: if lrightpit!=., vce(rob)
	matrix b=e(b)
	matrix m_left[`counter2',1]=b[1,1]
	local ++counter2

	mixed lrightpit1 p`i' loggdpt growtht unempt i.topic i.year || country: if lleftpit!=., vce(rob)
	matrix b=e(b)
	matrix m_right[`counter3',1]=b[1,1]
	local ++counter3
	}

matrix coef=m_all\m_left\m_right
svmat coef

egen group=seq() in 1/51, from(1) to(3) block(17)
egen incgroup=seq() in 1/51, from(1) to(17)

twoway line coef1 incgroup if group==1, lc(gs0) lw(medthick) ||				///
	line coef1 incgroup if group==2, lc(gs0) lw(medthick) lp(dash) ||		///
	line coef1 incgroup if group==3, lc(gs0) lw(medthick) lp(shortdash)		///
	xtitle(Income percentile, m(0 0 0 2)) xlab(1(2)17 1 "10" 3 "20" 5 "30"	///
	7 "40" 9 "50" 11 "60" 13 "70" 15 "80" 17 "90")							///
	ytitle(Regression coefficient, m(0 1 0 0)) scale(1.1)					///
	ylab(.007(.001).012 .01 ".010", ang(hor) nogrid)						///
	legend(order(1 "All" 2 "Left" 3 "Right") col(3) symx(9))				///
	plotr(m(3 3 2 2) lc(white)) graphr(m(3 2 2 3) fc(white) lc(white))

drop coef1-incgroup
